package com.wyzz.hr.bo.impl;

import java.math.BigDecimal;
import java.util.*;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IOvertimetxusedBO;
import com.wyzz.hr.dao.IOvertimetxusedDAO;
import com.wyzz.hr.pojo.Leaverequest;
import com.wyzz.hr.pojo.Overtimerequest;
import com.wyzz.hr.pojo.Overtimetxused;

public class OvertimetxusedBOImpl implements IOvertimetxusedBO {

	public OvertimetxusedBOImpl() {
	}

	public IOvertimetxusedDAO getOvertimetxusedDAO() {
		return overtimetxusedDAO;
	}

	public void setOvertimetxusedDAO(IOvertimetxusedDAO overtimetxusedDAO) {
		this.overtimetxusedDAO = overtimetxusedDAO;
	}

	public List searchOvertimetxusedByLrId(String lrId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Overtimetxused.class);
		dc.createAlias(Overtimetxused.PROP_OTU_OR_ID, "otuOrId", 1);
		dc.createAlias(Overtimetxused.PROP_OTU_LR_ID, "otuLrId", 1);
		dc.add(Restrictions
				.eq((new StringBuilder()).append(Overtimetxused.PROP_OTU_LR_ID)
						.append(".lrId").toString(), lrId));
		dc.addOrder(Order.desc(Overtimetxused.PROP_OTU_SEQ));
		return overtimetxusedDAO.findByCriteria(dc);
	}

	public void saveOrUpdate(List list) {
		overtimetxusedDAO.saveOrupdate(list);
	}

	public void delete(List list) {
		overtimetxusedDAO.deleteCollection(list);
	}

	public Map searchOvertimetxusedByLrIds(String lrIds[]) {
		if (lrIds == null || lrIds.length == 0)
			return null;
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Overtimetxused.class);
		dc.createAlias(Overtimetxused.PROP_OTU_OR_ID, "otuOrId", 1);
		dc.createAlias(Overtimetxused.PROP_OTU_LR_ID, "otuLrId", 1);
		dc.add(Restrictions.in((new StringBuilder()).append(
				Overtimetxused.PROP_OTU_LR_ID).append(".lrId").toString(),
				lrIds));
		dc.addOrder(Order.desc(Overtimetxused.PROP_OTU_SEQ));
		List list = overtimetxusedDAO.findByCriteria(dc);
		Map result = new HashMap();
		Overtimetxused otu;
		List oldOtuList;
		for (Iterator i$ = list.iterator(); i$.hasNext(); result.put(otu
				.getOtuLrId().getLrId(), oldOtuList)) {
			otu = (Overtimetxused) i$.next();
			if (!result.containsKey(otu.getOtuLrId().getLrId())) {
				List otuList = new ArrayList();
				result.put(otu.getOtuLrId().getLrId(), otuList);
			}
			oldOtuList = (List) result.get(otu.getOtuLrId().getLrId());
			oldOtuList.add(otu);
		}

		return result;
	}

	public double findOvertimerequestUsedTime(String orId) {
		String hql = (new StringBuilder())
				.append(
						"select sum(otu.otuHours) from Overtimetxused otu where otu.otuOrId.orId = '")
				.append(orId).append("'").toString();
		List sum = overtimetxusedDAO.exeHqlList(hql);
		if (sum == null || sum.size() < 1 || sum.get(0) == null)
			return 0.0D;
		else
			return Double.parseDouble(sum.get(0).toString());
	}

	public void rejectOrCancelLr(Leaverequest lrList[]) {
		if (lrList == null || lrList.length < 1 || lrList[0].getLrId() == null)
			return;
		String lrIds[] = new String[lrList.length];
		for (int i = 0; i < lrList.length; i++)
			lrIds[i] = lrList[i].getLrId();

		Map otuMap = searchOvertimetxusedByLrIds(lrIds);
		if (!otuMap.isEmpty()) {
			Leaverequest arr$[] = lrList;
			int len$ = arr$.length;
			for (int i$ = 0; i$ < len$; i$++) {
				Leaverequest lr = arr$[i$];
				if (!otuMap.containsKey(lr.getLrId()))
					continue;
				List otuList = (List) otuMap.get(lr.getLrId());
				List orList = new ArrayList();
				Overtimerequest or;
				for (Iterator it = otuList.iterator(); it.hasNext(); orList
						.add(or)) {
					Overtimetxused otu = (Overtimetxused) it.next();
					or = otu.getOtuOrId();
					or.setOrTiaoxiuHoursRemain(new BigDecimal(or
							.getOrTiaoxiuHoursRemain().doubleValue()
							+ otu.getOtuHours().doubleValue()));
				}

				lr.setUpdateOvertimerequestList(orList);
				lr.setDeleteOvertimetxUsedList(otuList);
			}

		}
	}

	private IOvertimetxusedDAO overtimetxusedDAO;
}
